math.randomseed(os.time())
stage1 = Image.load("Sprites/stage1.png")
bullet = Image.load("Sprites/kunail.png")
bulleet = Image.load("Sprites/chakra.png")
Resolution = { width = 480, height = 272 }
ninjasound = Sound.load("Sons/ninja.wav",false)

lefttable = { 
Image.load("./Sprites/Naruto/esquerda1.png"),
Image.load("./Sprites/Naruto/esquerda2.png"),
Image.load("./Sprites/Naruto/esquerda3.png"),
Image.load("./Sprites/Naruto/esquerda4.png"),
Image.load("./Sprites/Naruto/esquerda5.png"),
Image.load("./Sprites/Naruto/esquerda6.png"),
}

righttable = { 
Image.load("./Sprites/Naruto/direita1.png"),
Image.load("./Sprites/Naruto/direita2.png"),
Image.load("./Sprites/Naruto/direita3.png"),
Image.load("./Sprites/Naruto/direita4.png"),
Image.load("./Sprites/Naruto/direita5.png"),
Image.load("./Sprites/Naruto/direita6.png"),
}
standtable = { 
Image.load("./Sprites/Naruto/st1.png"),
Image.load("./Sprites/Naruto/st2.png"),
Image.load("./Sprites/Naruto/st3.png"),
Image.load("./Sprites/Naruto/st4.png"),
Image.load("./Sprites/Naruto/st5.png"),
Image.load("./Sprites/Naruto/st6.png"),
}

s = Image.load("./Sprites/Naruto/st1.png")
jump1 = Image.load ("./Sprites/Naruto/salto1.png")
jump2 = Image.load ("./Sprites/Naruto/salto2.png")
ss = Image.load("./Sprites/Enimigos/enimigo.png")

--Tabela do jogador
sd = { st1,st2,st3, st4, st5, st6 }
l = { l1,l2,l3,l4,l5,l6 }
r = { r1,r2,r3,r4,r5,r6 }
Player = { x = 30, y = 200, img = s, d = u, direction = "right", gy = 200, gravidade = 201, jumpstate = "ground", jumpspeed = 10, life = 100  }

--Tabela enimigo1
Enemyone = { x= 430, y = 127, life = 2, ecra = "true", img = ss, direction = "left" }


--Tabela enimigo2
Enemytwo = { x= 430, y = 55, life = 2, ecra = "true", img = ss, direction = "left" }

--Tabela enimigo3
Enemythree = { x= 350, y = 195, life = 2, ecra = "true", img = ss, direction = "left" }

--Tabela da bala
BulletInfo = {}
for a = 1,10 do
BulletInfo[a] = { pic = bulleet , firing = false, damage = 1, direction = "right", x = Player.x , y = Player.y }
end

--Tabela da bala
BulletInfo = {}
for a = 1,10 do
BulletInfo[a] = { pic = bullet , firing = false, damage = 1, direction = "right", x = Player.x , y = Player.y }
end

--Tabela da bala 1
BulletInfo1 = {}
for a = 1,10 do
BulletInfo1[a] = { pic = bullet , firing = false, damage = 1, direction = "left", x = Enemyone.x , y = Enemyone.y }
end

--Tabela da bala 2
BulletInfo2 = {}
for a = 1,10 do
BulletInfo2[a] = { pic = bullet , firing = false, damage = 1, direction = "left", x = Enemytwo.x , y = Enemytwo.y }
end

--Tabela da bala 3
BulletInfo3 = {}
for a = 1,10 do
BulletInfo3[a] = { pic = bullet , firing = false, damage = 1, direction = "left", x = Enemythree.x , y = Enemythree.y }
end

--Variaveis
oldpad = Controls.read()
currentBullet = 0
direction = "right"
animtimer = 0

--Funcoes

function bulletSetup()
dofile("./Script/Stage3/bulletsetup.txt")
end

function onebullet()
dofile("./Script/Stage3/onebullet.txt")
end

function twobullet()
dofile("./Script/Stage3/twobullet.txt")
end

function threebullet()
dofile("./Script/Stage3/threebullet.txt")
end

function playerFire()
dofile("./Script/Stage3/playerfire.txt")
end

function onefire()
dofile("./Script/Stage3/onefire.txt")
end

function twofire()
dofile("./Script/Stage3/twofire.txt")
end

function threefire()
dofile("./Script/Stage3/threefire.txt")
end

function playerjump()
if Player.jumpstate == "jumping" then 
Player.jumpspeed = Player.jumpspeed - 0.5 
Player.gravidade = Player.gravidade - Player.jumpspeed 
Player.img = jump 
end 

if Player.gravidade < 0 then 
Player.jumpstate = "falling" 
end 

if Player.gravidade < 190 and Player.jumpstate == "falling" then 
Player.gravidade = Player.gravidade + (Player.jumpspeed + 3) 
end 

if Player.gravidade == 200 then 
Player.jumpspeed = 10
Player.jumpstate = "ground" 
end 

if Player.gravidade > 200 
then Player.gravidade = 200 
end 

Player.y = Player.gravidade 
end

 function enemyfinalhit(BulletInfo, Enemy)
for h = 1,10 do
if BulletInfo[h].firing then
if Enemy.ecra == "true" and (BulletInfo[h].x + bullet:width() > Enemy.x) and (BulletInfo[h].x < Enemy.x + ss:width()) and (BulletInfo[h].y + bullet:height() > Enemy.y) and (BulletInfo[h].y < Enemy.y + ss:height()) then
BulletInfo[h].firing = false
Enemy.life = Enemy.life - 1
end
 if Enemy.life == 0 then
Enemy.ecra="false"
end
end
end
end

function playerhit(BulletInfo2, Enemyone)
for h = 1,10 do
if BulletInfo2[h].firing then
if (BulletInfo2[h].x + bullet:width() > Enemyone.x) and (BulletInfo2[h].x < Enemyone.x + ss:width()) and (BulletInfo2[h].y + bullet:height() > Enemyone.y) and (BulletInfo2[h].y < Enemyone.y + ss:height())  then
BulletInfo2[h].firing = false
Player.life = Player.life - 1
end
end
end
end

function animate(imgtable,interval) 
local numFrames = table.getn(imgtable) 
if animtimer >= numFrames*interval then animtimer = 0 end 
animtimer = animtimer + 1 
for i = 1, numFrames do 
if animtimer > interval*(i-1) and animtimer <= interval*i then 
img = imgtable[i] 
break 
end
end
return img 
end

--Loop principal
while true do
screen:clear()
pad = Controls.read()
Enemyone.AI = math.random (1,100)
Enemytwo.AI = math.random (1,100)
Enemythree.AI = math.random (1,100)

screen:blit(0,0, stage1 ,0, 0, stage1:width(), stage1:height(), false)

if pad:square() and oldpad:square() ~= pad:square() then
bulletSetup()
local sound = ninjasound
voice = sound:play()
end
if pad:cross() and not oldpad:cross() and Player.jumpstate == "ground" then
Player.jumpstate = "jumping"
end
playerjump()

if pad:left() and Player.x > 0 and Player.jumpstate == "ground" then
Player.x = Player.x - 1.5
Player.img = animate(lefttable,5)
direction="left"
else
Player.img = animate(standtable,15)
end
if pad:right() and Player.x < 500-Player.img:width() and Player.jumpstate == "ground" then
Player.x = Player.x + 1.5
Player.img = animate(righttable,10)
direction="right"
end
if pad:down() then
direction = "down"
end
if pad:up() then
direction = "up"
end

if Player.jumpstate == "jumping" and direction == "left" then 
Player.x = Player.x - 1.5
Player.img = jump2
end
if Player.jumpstate == "jumping" and direction == "right" then
Player.x = Player.x + 1.5
Player.img = jump1
end
if Player.jumpstate == "jumping" and direction == "up" then
Player.x = Player.x + 1.5
Player.img = jump1
end

if Player.x >= Resolution.width - Player.img:width()/2 then
System.memclean()
dofile ("./Script/Stage4/Stage4.txt")
end

screen:blit(Player.x,Player.y,Player.img)

playerFire()
onefire()
twofire()
threefire()

if Enemyone.ecra == "true" then
screen:blit(Enemyone.x,Enemyone.y,Enemyone.img)
end
if Enemytwo.ecra == "true" then
screen:blit(Enemytwo.x,Enemytwo.y,Enemytwo.img)
end
if Enemythree.ecra == "true" then
screen:blit(Enemythree.x,Enemythree.y,Enemythree.img)
end

if Enemyone.ecra == "true" then
if Enemyone.AI == 10 or Enemyone.AI == 30 then
onebullet()
end
end
if Enemytwo.ecra == "true" then
if Enemytwo.AI == 10 or Enemytwo.AI == 30 then
twobullet()
end
end
if Enemythree.ecra == "true" then
if Enemythree.AI == 10 or Enemythree.AI == 30 then
threebullet()
end
end

enemyfinalhit(BulletInfo, Enemyone) 
enemyfinalhit(BulletInfo, Enemytwo)
enemyfinalhit(BulletInfo, Enemythree)

playerhit(BulletInfo1, Player)
playerhit(BulletInfo2, Player)
playerhit(BulletInfo3, Player)

screen.waitVblankStart()
screen.flip()
oldpad = pad
end 